#
# Writes a matrix into dcsc format  header:JC:CP:(IR:NUM) where IR and NUM are interleaved for contiguous access
import random
import numpy as np
import scipy.sparse as ss
import numpy.random as npr
import struct
import ctypes

JC = list([0,6,7])
CP = list([0,2,3,4])
IR = list([5,7,3,1])
NUM = list([0.1,0.2,0.3,0.4])
size = 4*(4 + len(JC) + len(CP) + len(IR) + len(NUM))
bytes = ctypes.create_string_buffer(size)

offset = 0
for idx, val in enumerate(list([len(JC), len(CP) ,len(IR), len(NUM)])) :
	struct.pack_into('i', bytes, idx * 4, val)
	offset += 4;


for idx, _list in enumerate(list([JC, CP])) :
	for idx2, val in enumerate( _list ):
		struct.pack_into('i', bytes, offset, val )
		offset += 4

for idx, val in enumerate(CP[:len(CP)-1]) :
	start, end = val, CP[idx+1];
	print IR[start:end]
	for i in range(start,end) :
		struct.pack_into('i', bytes, offset, IR[i])
		offset += 4
		struct.pack_into('f', bytes, offset, NUM[i])
		offset += 4

f = open('dcsc_mat2.bin', 'wb')
f.write(bytes)
f.close()